<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>编辑文章</title>
    <style>
        /* 全局样式 */
        body {
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #e4f1fe 100%);
            margin: 0;
            padding: 0;
            color: #333;
            line-height: 1.6;
        }

        .container {
            max-width: 600px;
            margin: 60px auto;
            padding: 30px;
            background-color: #fff;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            border-radius: 15px;
            transition: transform 0.3s ease;
        }

        .container:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
        }

        /* 标题样式 */
        h2 {
            text-align: center;
            color: #3a7d44;
            margin-bottom: 30px;
            font-weight: 600;
            padding-bottom: 15px;
            border-bottom: 2px solid #e8f5e9;
        }

        /* 表单样式 */
        form {
            display: flex;
            flex-direction: column;
        }

        /* 标签样式 */
        label {
            margin-top: 10px;
            font-weight: 500;
            color: #555;
        }

        /* 输入框、文本域、下拉框样式 */
        input[type="text"],
        textarea,
        select {
            width: 100%;
            padding: 12px 15px;
            margin-top: 5px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 15px;
            transition: all 0.3s;
        }

        textarea {
            min-height: 200px;
            resize: vertical;
        }

        input[type="text"]:focus,
        textarea:focus,
        select:focus {
            border-color: #4caf50;
            outline: none;
            box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
        }

        /* 按钮样式 */
        .button-group {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-top: 20px;
        }

        button {
            padding: 12px 25px;
            border-radius: 6px;
            font-size: 15px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            border: none;
            flex: 1;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        /* 保存按钮 */
        button:nth-child(1) {
            background-color: #4caf50;
            color: white;
        }

        button:nth-child(1):hover {
            background-color: #388e3c;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(76, 175, 80, 0.3);
        }

        /* 发布按钮 */
        button:nth-child(2) {
            background-color: #2196f3;
            color: white;
        }

        button:nth-child(2):hover {
            background-color: #1976d2;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(33, 150, 243, 0.3);
        }

        /* 删除按钮 */
        button:nth-child(3) {
            background-color: #f44336;
            color: white;
        }

        button:nth-child(3):hover {
            background-color: #d32f2f;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(244, 67, 54, 0.3);
        }

        /* 图标样式 */
        .icon {
            margin-right: 8px;
            font-size: 18px;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                margin: 30px 15px;
                padding: 20px;
            }

            .button-group {
                flex-direction: column;
                align-items: center;
            }

            button {
                width: 100%;
                margin-bottom: 10px;
            }
        }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
</head>
<body>

<div class="container">
    <h2>欢迎来到博客系统</h2>
    <h2>修改文章页面</h2>
    <form id="articleForm">
        <input type="hidden" id="articleId" name="id" th:value="${article.id}" />
        <div>
            <label for="title"><i class="fas fa-heading icon"></i>标题:</label>
            <input type="text" id="title" name="title" th:value="${article.title}" required>
        </div>
        <div>
            <label for="content"><i class="fas fa-edit icon"></i>内容:</label>
            <textarea id="content" name="content" rows="10" required th:text="${article.content}"></textarea>
        </div>
        <div>
            <label for="categoryId"><i class="fas fa-folder icon"></i>分类:</label>
            <select id="categoryId" name="categoryId">
                <option th:each="category : ${categorys}"
                        th:value="${category.id}"
                        th:text="${category.name}"
                        th:selected="${category.id == article.categoryId}">
                </option>
            </select>
        </div>
        <div class="button-group">
            <button type="button" onclick="saveArticle(0)">
                <i class="fas fa-save icon"></i>保存
            </button>
            <button type="button" onclick="saveArticle(1)">
                <i class="fas fa-paper-plane icon"></i>发布
            </button>
            <button type="button" onclick="deleteArticle()">
                <i class="fas fa-trash-alt icon"></i>删除文章
            </button>
        </div>
    </form>
</div>

<script>
    function saveArticle(isApproved) {
        const title = document.getElementById('title').value;
        const content = document.getElementById('content').value;
        const articleId = document.getElementById('articleId').value;
        const categoryId = document.getElementById('categoryId').value;

        const articleData = {
            id: articleId,
            title: title,
            content: content,
            categoryId: categoryId,
            isApproved: isApproved
        };

        fetch('/article/edit', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(articleData)
        })
            .then(response => response.text())
            .then(data => {
                console.log(data);
                window.location.href = '/user/info';
            })
            .catch(error => console.error('Error:', error));
    }

    function deleteArticle() {
        const articleId = document.getElementById('articleId').value;
        if (!articleId) {
            alert('文章ID不存在，无法删除');
            return;
        }

        if (confirm('确定要删除这篇文章吗？')) {
            fetch(`/article/delete/${articleId}`, {
                method: 'post'
            })
                .then(response => response.text())
                .then(data => {
                    console.log(data);
                    window.location.href = '/user/info';
                })
                .catch(error => console.error('Error:', error));
        }
    }

    // 添加页面加载动画
    document.addEventListener('DOMContentLoaded', function() {
        document.body.style.opacity = '0';
        document.body.style.transition = 'opacity 0.5s ease';
        setTimeout(function() {
            document.body.style.opacity = '1';
        }, 50);
    });
</script>

</body>
</html>